package com.ambow.servlet;



import java.util.ArrayList;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import com.ambow.entity.User;

public class OnlineListener implements HttpSessionListener{
	
	public void sessionCreated(HttpSessionEvent arg0) {
	
		
			
		
	       ServletContext context = arg0.getSession().getServletContext();
	 
	       Integer count = (Integer) context.getAttribute("peopleOnline");
	       if (count == null) {
	           count = 1;
	       } else {
	          // count++;
	    	ArrayList alist =(ArrayList)context.getAttribute("list");
	    	count=alist.size();
	    	System.out.println("count+++:");
	       }
	       context.setAttribute("peopleOnline", count);
		 }
	
	 
	    public void sessionDestroyed(HttpSessionEvent arg0) {
	       ServletContext context = arg0.getSession().getServletContext();
	       
	       
	       String aName=(String)arg0.getSession().getAttribute("nameSession");
	       if(aName==null) {
	    	   return;
	       }
	       
	       ArrayList alist =(ArrayList)context.getAttribute("list");
	       if(alist==null) {
	    	   return;
	       }
	       
	       for(int i=0;i<alist.size();i++) {
	    	   if( alist.get(i)!=null && alist.get(i).equals(aName)) {
	    		   alist.remove(i);
	    		   return;
	    	   }
	       }
	       
	       
	       
	       /*
	       Integer count = (Integer) context.getAttribute("peopleOnline");
	 
	       count--;
	       context.setAttribute("peopleOnline", count);
	       */
	       
	    }

		

	

}
